/*
Copyright (c) 2011, Jeffrey Kunkel
All rights reserved.

Redistribution and use in source and binary forms, with or without 
modification, are permitted provided that the following conditions 
are met:

Redistributions of source code must retain the above copyright notice, 
this list of conditions and the following disclaimer.
Redistributions in binary form must reproduce the above copyright notice, 
this list of conditions and the following disclaimer in the documentation 
and/or other materials provided with the distribution.
Neither the name of the Jeffrey Kunkel nor the names of its contributors 
may be used to endorse or promote products derived from this software 
without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF 
THE POSSIBILITY OF SUCH DAMAGE.
*/

/// *************** TEST 1 *******************
#include <Listener.hpp>
#include <iostream>
namespace {
void hello() {
  std::cout<<"HELLO"<<std::endl;
}

int test1() {
  Listener0<> l;
  l.addListener( boost::function0<void>(hello) );
  l();
  l();

  return 0;
}
}
/// ******************************************

/// *************** TEST 2 *******************
#include <Listener.hpp>
#include <iostream>
#include <sstream>
#include <boost\ref.hpp>

namespace {
void goodbye( std::ostream & out ) {
  out<<"Bye"<<std::endl;
}

std::ostream & test2(std::ostream & out) {
  Listener1< std::ostream & > l;
  l.addListener( boost::function1<void, std::ostream & >(goodbye) );
  l(out);
  l(out);
  return out;
}

void test2() {
  std::stringstream ss;
  test2( ss );
  ss.seekp( std::ios::beg );
  std::copy(
    std::istreambuf_iterator<char>(ss),
    std::istreambuf_iterator<char>(),
    std::ostreambuf_iterator<char>(std::cout) );
}
}
/// *****************************************

/// *************** TEST 3 *******************
#include <Listener.hpp>
#include <iostream>
#include <sstream>
namespace {

struct test3AddFunctor {
  std::ostream & out;
  test3AddFunctor( std::ostream & out ) : out(out) {}
  void operator()( int x, int y ) {
    out<< "add(x="<<x<<", y="<<y<<")="
      <<"x+y=" << (x+y) << std::endl;
  }
};
struct test3MulFunctor {
  std::ostream & out;
  test3MulFunctor( std::ostream & out ) : out(out) {}
  void operator()( int x, int y ) {
    out<< "mul(x="<<x<<", y="<<y<<")="
      <<"x*y=" << (x*y) << std::endl;
  }
};

std::ostream & test3(std::ostream & out) {
  Listener2< int, int > l;
  l.addListener( boost::function2<void, int, int >( test3AddFunctor(out) ) );
  l.addListener( test3MulFunctor(out) );
  l(2,2);
  l(4,5);
  return out;
}

void test3() {
  std::stringstream ss;
  test3( ss );
  ss.seekp( std::ios::beg );
  std::copy(
    std::istreambuf_iterator<char>(ss),
    std::istreambuf_iterator<char>(),
    std::ostreambuf_iterator<char>(std::cout) );
}
}
/// *****************************************

/// *************** TEST 4 *******************
#include <Listener.hpp>
#include <iostream>
#include <sstream>
#include <boost\noncopyable.hpp>

namespace {
class test4Functor : boost::noncopyable {
public:
  std::stringstream ss;
  const std::string name;
  const int x,y;
  test4Functor( const std::string name, const int x, const int y ) : name(name), x(x), y(y) {}
  void operator()( const int a, const int b, const int c, const int d ) {
    const int k = (a+b+c+d);
    ss<< name
      << "( a="<<a
      << ", b="<<b
      << ", c="<<c
      << ", d="<<d
      << ", x="<<x
      << ", y="<<y
      << ") = " << std::endl;
    ss<< "case (a+b+c+d)> 0: k*(x+10)*y" << std::endl;
    ss<< "case (a+b+c+d)<=0: -k*(x-10)*y" << std::endl;
    ss<< "    =" 
      << ( (a+b+c+d)>0? ((a+b+c+d)*(x+10)*y) : -((a+b+c+d)*(x+10)*y) ) 
      << std::endl;
  }
};

void test4() {
  test4Functor f( "test4 f", 15, 35 );
  test4Functor g( "test4 g", 5, 8 );
  Listener4< const int, const int, const int, const int > l;
  l.addListener( boost::ref(f) );
  l.addListener( boost::ref(g) );
  l( 1,2,3,4 );
  l( 1,-2,-3,-4 );
  
  f.ss.seekp( std::ios::beg );
  std::copy(
    std::istreambuf_iterator<char>(f.ss),
    std::istreambuf_iterator<char>(),
    std::ostreambuf_iterator<char>(std::cout) );
  g.ss.seekp( std::ios::beg );
  std::copy(
    std::istreambuf_iterator<char>(g.ss),
    std::istreambuf_iterator<char>(),
    std::ostreambuf_iterator<char>(std::cout) );
}
}
/// *****************************************

/// *************** TEST 5 *******************
#include <Listener.hpp>
#include <iostream>
#include <sstream>
#include <boost\noncopyable.hpp>

namespace {
class test5Functor {
public:
  std::ostream & out;
  const std::string name;
  const int x,y;
  test5Functor( std::ostream & out, const std::string name, const int x, const int y ) 
    : out(out), name(name), x(x), y(y) {}
  void operator()( const int a, const int b, const int c, const int d ) {
    const int k = (a+b+c+d);
    out<< name
      << "( a="<<a
      << ", b="<<b
      << ", c="<<c
      << ", d="<<d
      << ", x="<<x
      << ", y="<<y
      << ") = " << std::endl;
    out<< "case (a+b+c+d)> 0: k*(x+10)*y" << std::endl;
    out<< "case (a+b+c+d)<=0: -k*(x-10)*y" << std::endl;
    out<< "    =" 
      << ( (a+b+c+d)>0? ((a+b+c+d)*(x+10)*y) : -((a+b+c+d)*(x+10)*y) ) 
      << std::endl;
  }
};

typedef Listener4< const int, const int, const int, const int > Listener;

void BADaddToListener(Listener & l) {
  test5Functor f( std::cout, "test5 f", 15, 35 );
  test5Functor g( std::cout, "test5 g", 5, 8 );

  /// Reference parameters disappear after this function
  /// returns. However l still sees the objects.
  l.addListener( boost::ref(f) );
  l.addListener( boost::ref(g) );
}
void test5BAD() {
  Listener l;
  BADaddToListener(l);
  {
    // stack alloc and overwrite bad alloc space.
    char buffer[1024];
    std::fill( buffer, buffer + 1024, (char)0 );
  }
  l( 1,2,3,4 ); 
  l( 1,-2,-3,-4 );
}

void GoodaddToListener(Listener & l) {
  test5Functor f( std::cout, "t5 f", 15, 35 );
  test5Functor g( std::cout, "t5 g", 5, 8 );

  /// No reference parameters mean the functors were
  /// copied into addListener.
  l.addListener( f );
  l.addListener( g );
}
void test5GOOD() {
  Listener l;
  GoodaddToListener(l);
  {
    // stack alloc and overwrite bad alloc space if it exists.
    char buffer[1024];
    std::fill( buffer, buffer + 1024, (char)0 );
  }
  l( 1,2,3,4 ); 
  l( 1,-2,-3,-4 );
}

void test5() {
  /// Try test5BAD and see it either seg fault or some
  /// trash will result. Perhaps it will succeed with
  /// some luck!
  test5GOOD();
  //test5BAD();
}
}
/// *****************************************

/// Test 6
#include <Listener.hpp>
#include <string>
#include <iostream>
namespace {
void cout_string( std::string & s ) {
  std::cout<<  s  <<std::endl;
}
class foo : public Listener1<std::string&> {
public:
  void operator()( std::string s ) {
    std::string fooed = "foo+";
    fooed.append(s);
    Listener1<std::string&>::operator()(fooed);
  }
};
class bar : public Listener1<std::string&> {
};

void test6() {
  foo f;
  f.addListener( cout_string );
  f("hello");
  f(" my ");
  f(" dear ");
  f(" aunt ");
  f(" sally. ");

  bar b;
  b.addListener( cout_string );
  std::string s;
  s = "Hello my dear aunt sally.";
  b(s);
  return;
}
}
///

void TestListeners() {
  test1();
  test2();
  test3();
  test4();
  test5();
  test6(); 
}

int main() {
  TestListeners();
  return 0;
}
